import api from './api';

export interface TraceabilityQuery {
  pageIndex?: number;
  pageSize?: number;
  productName?: string;
  productionBatchStart?: string;
  productionBatchEnd?: string;
  shelfLifeMin?: number;
  shelfLifeMax?: number;
  productionLicenseNumber?: string;
  productionCompany?: string;
}

export interface TraceabilityItem {
  id: number;
  traceabilityCode: string;
  productName: string;
  productionBatch: string;
  shelfLife: number;
  productionLicenseNumber: string;
  productionCompany: string;
  coverImage?: string;
  completenessScore: number;
  createTime: string;
  updateTime?: string;
}

export interface CreateTraceabilityDTO {
  traceabilityCode: string;
  productName: string;
  productionBatch: string;
  shelfLife: number;
  productionLicenseNumber: string;
  productionCompany: string;
  coverImage?: string;
}

export interface UpdateTraceabilityDTO extends CreateTraceabilityDTO {
  id: number;
}

export interface ApiResult<T> {
  code: number;
  message: string;
  data: T;
}

export interface ApiPagin<T> {
  totalCount: number;
  totalPages: number;
  pageIndex: number;
  pageSize: number;
  items: T[];
}

const traceabilityService = {
  // 获取分页列表
  getPagedTraceability: (query: TraceabilityQuery) => {
    return api.get<any, ApiResult<ApiPagin<TraceabilityItem>>>('/Traceability/paged', { params: query });
  },

  // 获取单条记录
  getTraceabilityById: (id: number) => {
    return api.get<any, ApiResult<TraceabilityItem>>(`/Traceability/${id}`);
  },

  // 根据溯源码获取记录
  getTraceabilityByCode: (code: string) => {
    return api.get<any, ApiResult<TraceabilityItem>>(`/Traceability/code/${code}`);
  },

  // 创建记录
  createTraceability: (data: CreateTraceabilityDTO) => {
    return api.post<any, ApiResult<TraceabilityItem>>('/Traceability', data);
  },

  // 更新记录
  updateTraceability: (id: number, data: UpdateTraceabilityDTO) => {
    return api.put<any, ApiResult<TraceabilityItem>>(`/Traceability/${id}`, data);
  },

  // 删除记录
  deleteTraceability: (id: number) => {
    return api.delete<any, ApiResult<void>>(`/Traceability/${id}`);
  },

  // 批量删除
  batchDeleteTraceability: (ids: number[]) => {
    return api.post<any, ApiResult<void>>('/Traceability/batch-delete', ids);
  }
};

export default traceabilityService; 